<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子传父</title>
    <style>
    </style>
</head>
<body>
    <div id="app">
        <jery></jery>
        <tom></tom>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>

    <script>
        var hub = new Vue();
        Vue.component('jery', {
            props: [],
            data: function () {
                return {
                    num: 20
                }
            },
            template: `<div>
                <h3>{{num}}</h3>
                <button @click='handle'>改 tom</button>
            </div>`,
            methods: {
                handle() {
                    hub.$emit('tom-event', 1)
                }
            },
            mounted() {
                hub.$on('jery-event', (v) => {
                    this.num -= v;
                });
            }
        });

        Vue.component('tom', {
            props: [],
            data: function () {
                return {
                    num: 10
                }
            },
            template: `<div>
                <h3>{{num}}</h3>
                <button @click='handle'>改 jery</button>
            </div>`,
            methods: {
                handle() {
                    // Handle logic for changing jery
                    hub.$emit('jery-event', 1)
                }
            },
            mounted() {
                hub.$on('tom-event', (v) => {
                    this.num += v;
                });
            }
        });

        new Vue({
            el: '#app'
        });
    </script>
</body>
</html>
